var express = require('express');
var db2 = require('./db2.js')
var router = express.Router()
var bodyParser = require('body-parser');
var md5 = require('md5-nodejs');
var urlencodedParser = bodyParser.urlencoded({ extended: false })

var router = express.Router()


router.get('/', function (req, res) {
    res.send('msbd')
})
router.get('/list', function (req, res) {
    let { pages, pagesize, orderby = 'id', sort = 'desc', keyword } = req.query
    if (!pages) {
        pages = 0
    }
    if (!pagesize) {
        pagesize = 20
    }
    let sum = pages * pagesize

    var sql = `select m.id,m.title,m.content,m.author,m.cid,m.time,m.date,c.name as type from msbd as m,classify as c where m.cid=c.id`;
    var sqla = `select count(*) as total from msbd`;

    if (keyword) {
        sql += ` and title like '${keyword}%'`
        sqla += ` where title like '${keyword}%'`
    }
    if (orderby) {
        sql += ` order by ${'m.' + orderby} ${sort}`
    }
    console.log(sql);
    // if (orderby) {
    //     sql += ` order by ${orderby} ${sort}`
    // }
    sql += ` limit ${sum},${pagesize}`
    // db2.doSql(sql).then(data => {
    //     console.log(data);
    // })
    // console.log(p1);
    var p1 = db2.doSql(sql)
    var p2 = db2.doSql(sqla)
    Promise.all([p1, p2]).then(data => {
        // res.header("Access-Control-Allow-Origin", "*");
        // res.header("Content-Type", "text/html; charset=utf-8")
        // console.log(JSON.stringify(db2.heal(data)));
        console.log(data[1].resultes[0].total);
        res.send(JSON.stringify(db2.heal(data[0], data[1].resultes[0].total)))
    })
})

//新增
router.post('/save', urlencodedParser, function (req, res) {
    let { title, content, author, time, cid } = req.body
    var sql = `insert into msbd (title,content,author,time,cid) values ('${title}','${content}','${author}','${time}',${cid})`;
    db2.doSql(sql).then(data => {
        // console.log(data);
        // res.header("Access-Control-Allow-Origin", "*");
        // res.header("Content-Type", "text/html; charset=utf-8")
        res.send(db2.hand(data[0]))
    })
})

//修改
router.post('/update', urlencodedParser, function (req, res) {
    let { id, title, content, author, type, time } = req.body
    var sql = `update msbd set title='${title}',content='${content}',author='${author}',time='${time}' where id=${id}`
    db2.doSql(sql).then(data => {
        // res.header("Access-Control-Allow-Origin", "*");
        // res.header("Content-Type", "text/html; charset=utf-8")
        res.send(db2.hand(data[0]))
    })

})

//删除
router.get('/delete', function (req, res) {
    // console.log(req.query);
    let id = req.query.id
    // console.log(id);
    sql = `delete from msbd where id in(${id})`;
    db2.doSql(sql).then(data => {
        // res.header("Access-Control-Allow-Origin", "*");
        // console.log(data);
        res.send(db2.heal(data[0]))
    })
    // // res.header("Content-Type", "text/html; charset=utf-8")

})




// var server = app.listen(1111, function () {
//     var host = server.address().address
//     var port = server.address().port
//     console.log("应用实例，访问地址为 http://%s:%s", host, port)
// })
module.exports = router